import axios from 'axios'
import { getCookieFromStorage } from '@/common/js/cookieHelper'

axios.defaults.withCredentials = true

export const addAuthenticationHeader = () => {
  return {
    'authentication': 'Bearer ' + getCookieFromStorage('PCRS_JWT_ACCESS_TOKEN').cookieValue
  }
}

export const get = async (url, params) => await axios.get(url, {
  headers: addAuthenticationHeader(),
  params: {
    ...params
  }
})

export const post = async (url, params) => await axios.request({
  headers: addAuthenticationHeader(),
  url: url,
  method: 'POST',
  data: params
})

export const del = async (url, params) => await axios.delete(url, {
  headers: addAuthenticationHeader(),
  params: {
    ...params
  }
})

export const put = async (url, data) => await axios.request({
  headers: addAuthenticationHeader(),
  url: url,
  method: 'PUT',
  data: data
})

export const download = async (url, params) => await axios.get(url, {
  headers: addAuthenticationHeader(),
  responseType: 'blob',
  params: {
    ...params
  }
})
